package com.linzm.core;

import com.linzm.aspect.AspectHandler;
import com.linzm.aspect.IAspectHandler;
import com.linzm.context.IocApplicationContext;
import com.linzm.entry.CheckResult;
import javafx.util.Pair;

import java.util.Map;

/**
 * @Date: 2023/11/9 19:16
 * @User linziming
 */
public class AopBeanContext {
    private static IAspectHandler aspectHandler = null;

    static {
        init();
    }

    public static Object getObject(Class clz) {
        Object target = IocApplicationContext.getObject(clz);
        if (target == null) {
            throw new RuntimeException("容器中获取不到实例");
        }
        Pair<Object, CheckResult> aspectResult = aspectHandler.getAspectInstance(clz.getTypeName());
        // 没有切面
        if (aspectResult == null) {
            return target;
        }
        // 创建代理类
        return ProxyBeanFactory.newProxyBean(target, aspectResult);
    }

    private static void init() {
        createProxyBeanContext(IocApplicationContext.getClzNameBeanMap());
    }

    // 根据切点，创建有代理类的bean容器
    public static void createProxyBeanContext(Map<String, Object> iocMap) {
        // 获取ioc容器 然后遍历找到切面类
        aspectHandler = new AspectHandler(iocMap);
    }
}
